import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  knowledgeList: {},
  currentKnowledge: {
    content: "",
    image: "",
    provider: "",
    title: "",
    opt: "",
    video: "",
    cover: "",
    status: 3
  },
  searchKnowledgeCondition: {
    pageSize: 10,
    title: "",
    status: ""
  }
};

const getters = {
  knowledgeList: state => state.knowledgeList,
  currentKnowledge: state => state.currentKnowledge,
  searchKnowledgeCondition: state => state.searchKnowledgeCondition
};

const actions = {
  getKnowledgePageS({ commit }, param) {
    return Api.getKnowledgePage(param).then(ret => {
      commit(types.UPDATE_KNOWLEDGE_LIST, ret.data.data);
      return ret;
    });
  },
  getKnowledgeS({ commit }, id) {
    return Api.getKnowledge({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_KNOWLEDGE, ret.data.data);
      return ret;
    });
  },
  addOrUpdateKnowledgeS({ commit }, payload) {
    return Api.addOrUpdateKnowledge({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteKnowledgeS({ commit }, param) {
    return Api.deleteKnowledge(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_KNOWLEDGE](state, payload) {
    state.currentKnowledge = {
      content: "",
      image: "",
      provider: "",
      title: "",
      opt: "",
      video: "",
      cover: "",
      status: 3
    };
  },
  [types.UPDATE_CURRENT_KNOWLEDGE](state, payload) {
    state.currentKnowledge = payload;
  },
  [types.UPDATE_KNOWLEDGE_LIST](state, payload) {
    state.knowledgeList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
